<script>
  import config from './config'
  import store from '@/store'
  import {
    getToken,
    checkWhite
  } from '@/utils/auth'

  // #ifdef H5
  import signalR from '@/utils/signalR'
  // #endif

  export default {
    onLaunch: function() {
      this.initApp()
    },
    computed: {
      token() {
        return this.$store.getters.token
      }
    },
    watch: {
      token: {
        immediate: true,
        handler: function(val) {
          var that = this
          if (val) {
            // #ifdef H5
            that.$nextTick(() => {
              signalR.start();
            })
            // #endif
          }
        },
      },
    },
    computed: {
      token() {
        return this.$store.getters.token
      }
    },
    methods: {
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
      },
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
        var whiteList = ['#/pages/socialLogin', '#/pages/register']
        if (!getToken() && !checkWhite(window.location.hash, whiteList)) {
          var url = window.location.href
          var redirectUrl = ''

          if (url.indexOf('#') !== -1 && url.indexOf('/pages/index') != -1) {
            redirectUrl = url.substr(url.indexOf('#') + 1)
          }

          this.$tab.reLaunch('/pages/login?redirectUrl=' + redirectUrl)
        }
      }
    }
  }
</script>

<style lang="scss">
  /* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
  @import "@/uni_modules/uview-ui/index.scss";
  @import '@/static/scss/index.scss'
</style>